package com.homevip.core.util;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 时间范围
 * @author ll
 *
 */
public class TimeRange implements Serializable {

	/** 序列号 */
	private static final long serialVersionUID = 1L;
	
	// 格式
	private SimpleDateFormat format;
	// 开始日期
	private FastDate start;
	// 结束日期
	private FastDate end;
	// 小时数
	private double hourCount = 0;
	
	public String getStart() {
		
		return format.format(this.start);
	}
	
	public Date getStartTime() {
		
		return this.start;
	}

	public String getEnd() {
		
		return format.format(this.end);
	}
	
	public Date getEndTime() {
		
		return this.end;
	}

	public void setStart(String start) {

		this.start = new FastDate(start);
	}

	public void setEnd(String end) {

		this.end = new FastDate(end);
	}

	public void setFormat(String format) {
		
		this.format = new SimpleDateFormat(format);
	}
	
	/**
	 * 取开始日期
	 * @return
	 */
	public String getStartDate() {

		return this.start.toDateText();
	}
	
	/**
	 * 取开始小时
	 * @return
	 */
	public int getStartHour() {
		
		Calendar cal = Calendar.getInstance();
		cal.setTime(start);
		return cal.get(Calendar.HOUR_OF_DAY);
	}

	/**
	 * 指定日期范围
	 * @param start
	 * @param end
	 */
	public TimeRange(Date start, Date end) {
		
		format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		this.start = new FastDate(start.getTime());
		this.end = new FastDate(end.getTime());
	}
	
	/**
	 * 指定日期范围
	 * @param start
	 * @param end
	 */
	public TimeRange(String start, String end) {
	
		format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		this.start = new FastDate(start);
		this.end = new FastDate(end);
	}
	
	/**
	 * 指定日期范围
	 * @param start
	 * @param end
	 */
	public TimeRange(String date, String start, String end) {
	
		format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		this.start = new FastDate(date + " " + start);
		this.end = new FastDate(date + " " + end);
	}
	
	/**
	 * 指定日期范围
	 * @param date
	 * @param hours 小时
	 * @param amount >0向后，<0向前
	 */
	public TimeRange(Date date, int hours) {
		
		format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		init(date.getTime(), Calendar.HOUR_OF_DAY, hours);
	}
	
	/**
	 * 生成范围
	 * @param base
	 * @param field
	 * @param amount
	 */
	private void init(long base, int field, int amount) {

		FastDate now = new FastDate(base);
		
		if (Calendar.HOUR_OF_DAY == field) {
			
			// 小时
			hourCount = Math.abs(amount);
			if (amount > 0) {
				
				// 后
				this.start = new FastDate(now.getTime());
				this.end = now.AddDate(Calendar.HOUR_OF_DAY, amount);
			}
			else {
				
				// 前
				now = now.AddDate(Calendar.HOUR_OF_DAY, 1);
				this.end = new FastDate(now.getTime());
				this.start = now.AddDate(Calendar.HOUR_OF_DAY, amount);
			}
		}
	}

	/**
	 * 取小时数
	 * @return
	 */
	public double getHourCount() {
		
		if (0 == hourCount) {

			// 计算
			hourCount = this.end.compareTo(FastDate.MINUTE, this.start) / 60.0;
		}
		//FIXME 临时兼容半小时产品
		return Math.ceil(hourCount) < 1 ? 1 : Math.ceil(hourCount);
	}
}
